package com.kg.sdk.view;

import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

import com.example.kpgamesdk.R;
import com.kg.sdk.activity.KPUserCenterActivity;

public class KPFloatView {
    WindowManager.LayoutParams params;
    //实例化的WindowManager.
    WindowManager windowManager;
    private Context mContext;
    private View floatView;
    private boolean isShow = false;
    public KPFloatView(Context context){
        mContext = context;
        params = new WindowManager.LayoutParams();
        windowManager = (WindowManager)context. getSystemService(Context.WINDOW_SERVICE);
        //设置type.系统提示型窗口，一般都在应用程序窗口之上.
        //这个是8.0系统以下的
//        params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        //这个是8.0系统以上
       // params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
    
        //设置效果为背景透明.
        params.format = PixelFormat.RGBA_8888;
        //设置flags.不可聚焦及不可使用按钮对悬浮窗进行操控.
        params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

        //设置窗口初始停靠位置.
       // int height = context.getResources().getDisplayMetrics().heightPixels/2;
        params.gravity = Gravity.LEFT | Gravity.CENTER_VERTICAL;
        params.x = 0;
       // params.y = height;

        //设置悬浮窗口长宽数据.
        //注意，这里的width和height均使用px而非dp.这里我偷了个懒
        //如果你想完全对应布局设置，需要先获取到机器的dpi
        //px与dp的换算为px = dp * (dpi / 160).
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        floatView  = LayoutInflater.from(context).inflate(R.layout.float_sdk_layout,null);
        ImageView imgLogo = (ImageView)floatView.findViewById(R.id.imgLogo);
        imgLogo.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setClass(mContext, KPUserCenterActivity.class);
                mContext.startActivity(intent);
            }
        });

        floatView.setOnTouchListener(new View.OnTouchListener() {
            int lastX, lastY;
            int paramX, paramY;
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        lastX = (int) event.getRawX();
                        lastY = (int) event.getRawY();
                        paramX = params.x;
                        paramY = params.y;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        int dx = (int) event.getRawX() - lastX;
                        int dy = (int) event.getRawY() - lastY;
                        params.x = paramX + dx;
                        params.y = paramY + dy;
                        // 更新悬浮窗位置
                        windowManager.updateViewLayout(floatView, params);
                        break;
                }
                return true;
            }
        });
    }

    public void showFloatView(){
        if(!isShow){
            windowManager.addView(floatView,params);
            isShow = true;
        }

    }

    public void removFloatView(){
        if(isShow){
            isShow = false;
            windowManager.removeView(floatView);
        }
    }

}
